package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberDemandBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberDemandBean;
import com.sgb.member.entity.MemberDemandLeaseBean;
import com.sgb.member.ui.activity.MemberDemandActivity;
import com.sgb.member.ui.adapter.MemberDemandAdapter;
import com.sgb.member.ui.adapter.MemberDemandLeaseAdapter;
import com.sgb.member.view_model.MemberDemandModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * 會員需求
 */
public class MemberDemandFragment extends AppLazyFragment {

    @Inject
    MemberDemandModel sModel;
    private FragmentMemberDemandBinding mBind;
    private MemberDemandAdapter mAdapter;
    private MemberDemandLeaseAdapter mAdapterLease;
    private int mCurPos;
    private String mNo = "";

    public static MemberDemandFragment newInstance(int type) {
        MemberDemandFragment fragment = new MemberDemandFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_member_demand, container, false);
        return mBind.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        mBind.tabLayout.setVisibility(View.GONE);
        initRecycler();
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
    }

    public void loadData(boolean isLoadMore) {
        // mCurPos==0为商品询价 mCurPos==1为求租需求
        String text = "";
        if (getActivity() != null) {
            text = ((MemberDemandActivity) getActivity()).getSearchText();
        }
        // 商品没有筛选  租赁有筛选
        if (mCurPos == 1) {
            if (mBind.tabLayout.getTabCount() == 0) {
                sModel.getAllLeaseType();
            } else {
                sModel.getDemandList(isLoadMore, mCurPos, text, mNo);
            }
        } else {
            sModel.getDemandList(isLoadMore, mCurPos, text, mNo);
        }
    }

    /**
     * -------------------------------------initRecycler------------------------------------------
     */
    private void initRecycler() {
        TextView textView = mBind.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBind.recyclerView.setPullRefreshEnabled(true);
        mBind.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBind.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");

        mBind.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBind.recyclerView.setLayoutManager(layoutManager);
        switch (mCurPos) {
            case 0:
                mAdapter = new MemberDemandAdapter(getActivity(), mCurPos, null);
                mBind.recyclerView.setAdapter(mAdapter);
                mAdapter.setOnItemClick(new AdapterOnItemClick<MemberDemandBean>() {
                    @Override
                    public void onItemClick(MemberDemandBean s, int position) {
                        sModel.webH5(mCurPos, s.getEnquiryNo(), mBind.tabLayout.getSelectedTabPosition());
                    }
                });
                break;
            case 1:
                mAdapterLease = new MemberDemandLeaseAdapter(getActivity(), mCurPos, null);
                mBind.recyclerView.setAdapter(mAdapterLease);
                mAdapterLease.setOnItemClick(new AdapterOnItemClick<MemberDemandLeaseBean>() {
                    @Override
                    public void onItemClick(MemberDemandLeaseBean s, int position) {
                        sModel.webH5(mCurPos, s.getNo(), mBind.tabLayout.getSelectedTabPosition());
                    }
                });
                break;
        }
    }

    public void closeRecyclerRefreshing() {
        mBind.recyclerView.refreshComplete();
        mBind.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        closeRecyclerRefreshing();
        if (mCurPos == 0) {
            mAdapter.clearDatas();
        } else {
            mAdapterLease.clearDatas();
        }
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<MemberDemandBean> list, boolean isLoadMore, boolean noMoreData) {
        mBind.recyclerView.setVisibility(View.VISIBLE);
        mBind.recyclerView.setLoadingMoreEnabled(!noMoreData);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBind.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBind.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void refreshRecyclerLease(List<MemberDemandLeaseBean> list, boolean isLoadMore, boolean noMoreData) {
        mBind.recyclerView.setVisibility(View.VISIBLE);
        mBind.recyclerView.setLoadingMoreEnabled(!noMoreData);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapterLease.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBind.emptyView.setVisibility(View.GONE);
                mAdapterLease.setDatas(list);
            } else {
                mAdapterLease.clearDatas();
                mBind.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setChooseItemData(List<CommonPopEntity> data) {
        mBind.tabLayout.setVisibility(View.VISIBLE);
        for (CommonPopEntity datum : data) {
            mBind.tabLayout.addTab(mBind.tabLayout.newTab().setText(datum.getTypeName()));
        }
        mBind.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBind.tabLayout.setNormalColor(R.color.color_title_small);
        mBind.tabLayout.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mNo = data.get(tab.getPosition()).getNo();
                loadData(false);
            }
        });
        mNo = data.get(0).getNo();
        loadData(false);
    }
}
